function Node(val, left, right) {
  this.val = val;
  this.left = left;
  this.right = right;
}

function buildTree(root, level) {
  if (root.val === level) {
    return;
  }
  let val = root.val;
  let left = new Node(val + 1, null, null);
  let right = new Node(val + 1, null, null);
  root.left = left;
  root.right = right;
  buildTree(left, level);
  buildTree(right, level);
  return root;
}

let root = new Node(0, null, null);
let totalLevel = 4;
let node = buildTree(root, totalLevel);
console.log(node);

function bfs(root) {
  const queue = [root];
  const res = [];
  while (queue.length) {
    let len = queue.length;
    res.push([]);
    while (len--) {
      let node = queue.shift();
      res[res.length-1].push(node.val);
      node.left && queue.push(node.left);
      node.right && queue.push(node.right);
    }
  }
  return res;
}

console.log(bfs(node));